package com.dongzili.common.config.aspect;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync // 启用异步功能
public class AsyncConfig {

    @Bean(name = "logExecutor")
    public Executor logExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2); // 核心线程数
        executor.setMaxPoolSize(2); // 最大线程数
        executor.setQueueCapacity(Integer.MAX_VALUE); // 队列容量
        executor.setKeepAliveSeconds(300); // 线程空闲时间
        executor.setThreadNamePrefix("log-"); // 线程名前缀
        executor.setWaitForTasksToCompleteOnShutdown(true); // 等待任务完成再关闭
        executor.setAwaitTerminationSeconds(60); // 等待终止时间
        executor.initialize();
        return executor;
    }
}